package com.group3.admin.producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.group3.admin.util.JsonUtil;
import com.group3.entity.AMerchant;

public class Producer {
	ConnectionFactory cf = null;
	Connection conn = null;
	Session session = null;
	MessageProducer producer = null;

	public void tellMBlock(AMerchant aMerchant) throws JMSException {
		try {
			System.out.println("in here   ");
			aMerchant.setmImage("");
			aMerchant.setIdCard("");
			System.out.println("aMerchant is " + aMerchant);
			cf = new ActiveMQConnectionFactory("failover://tcp://10.222.29.150:61616");
			conn = cf.createConnection();
			System.out.println("conn is " + conn);
			String jsonAmer = JsonUtil.getInstance().ObjectToJson(aMerchant);
			System.out.println("jsonAmer " + jsonAmer);
			session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); 
			Destination queue = new ActiveMQQueue("Admin-Merchant52");
		
			producer = session.createProducer(queue);
			conn.start();
			System.out.println("producer is " + producer);
			TextMessage tm = session.createTextMessage(jsonAmer);
			System.out.println("tm is " +tm.toString());
			producer.send(tm);
			System.out.println("conn is " + conn);
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("producer error ");
		} finally {
			session.commit();
			session.close();
			producer.close();
			conn.close();
		}
	}
}
